package com.zhuss.food.processor;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.zhuss.bean.vo.core.DetailImportVo;
import com.zhuss.bean.vo.food.order.ListOrderVo;
import com.zhuss.exception.WpsException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.ExcelUtil;
import com.zhuss.util.NoUtil;

@Component
public class OrderExport {
	
	@Value("${filePath}")
    private String filePath;
	
	@Value("${fileReadPath}")
	private String fileReadPath;
	
	public Object process(List<ListOrderVo> orderList) throws WpsException {
		if (orderList.isEmpty()) {
			orderList.add(new ListOrderVo());
		}
		String url = "";
		String path = filePath + "batch\\order\\";
		String readPath = fileReadPath +  "batch\\order\\";
		String fileName = "";
		fileName = NoUtil.gen();
		File file2 = new File(path);
		if (!file2.exists()) {
			file2.mkdirs();
		}
		LinkedHashMap<String,String> map = ExcelUtil.getExportOrderMap();
		File file3 = new File(path + "订单导出-" + fileName + ".xls");
		OutputStream os = null;
		try {
			os = new FileOutputStream(file3);
			ExcelUtil.listToExcel(orderList, map, "订单导出", os);
		} catch (Exception e) {
			e.printStackTrace();
			throw new WpsException(ExceptionCode.导出失败);
		}
		
		url = readPath + file3.getName();
		
		DetailImportVo result = new DetailImportVo();
		result.setUrl(url);
		return result;
	}

}
